package com.nazca.tvremote.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;

import com.nazca.tvremote.R;
import com.nazca.tvremote.event.RemoteKeyEvent;
import com.nazca.tvremote.service.Controller;
import com.nazca.tvremote.view.TitleBar;

public class GameRemoteActivity extends Activity {
	
	private TitleBar mTitleBar;
	private ViewStub mNumStub;
	private View mNumView;
	
	private Button mLeftBtn;
	private Button mRightBtn;
	private Button mUpBtn;
	private Button mDownBtn;
	
	private Button mHideBtn;
	private Button mShowBtn;
	private View mNumPad;
	
	private Button mABtn;
	private Button mBBtn;
	private Button mCBtn;
	private Button mDBtn;
	private Button mBackBtn;
	private Button mExitBtn;
	private Button mEnterBtn;
	
	private Controller mController;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_remote);
		
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mTitleBar.getBackButton().setText(R.string.main_game_remote);
		mTitleBar.getBackButton().setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				finish();
			}
		});
		
		mNumStub = (ViewStub) findViewById(R.id.view_stub_num);
		mNumView = mNumStub.inflate();
		
		initDirectionBtn();
		initNumBtn();
		initOptionsBtn();
		initShowHide();
		
		mController = Controller.getInstance(getApplicationContext());
	}

	private void initShowHide(){
		mHideBtn = (Button) findViewById(R.id.hide);
		mShowBtn = (Button) findViewById(R.id.show);
		mNumPad = findViewById(R.id.num_pad);
		
		mHideBtn.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				mNumPad.setVisibility(View.GONE);
				mShowBtn.setVisibility(View.VISIBLE);
//				mHideBtn.setVisibility(View.GONE);
			}
		});
		mShowBtn.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				mNumPad.setVisibility(View.VISIBLE);
				mShowBtn.setVisibility(View.GONE);
//				mHideBtn.setVisibility(View.VISIBLE);
			}
		});
	}
	
	private void initOptionsBtn(){
		mABtn = (Button) findViewById(R.id.a);
		mBBtn = (Button) findViewById(R.id.b);
		mCBtn = (Button) findViewById(R.id.c);
		mDBtn = (Button) findViewById(R.id.d);
		
		mBackBtn = (Button) findViewById(R.id.back);
		mExitBtn = (Button) findViewById(R.id.exit);
		mEnterBtn = (Button) findViewById(R.id.enter);
		
		mABtn.setOnClickListener(mOptionsOnClickListener);
		mBBtn.setOnClickListener(mOptionsOnClickListener);
		mCBtn.setOnClickListener(mOptionsOnClickListener);
		mDBtn.setOnClickListener(mOptionsOnClickListener);
		mBackBtn.setOnClickListener(mOptionsOnClickListener);
		mExitBtn.setOnClickListener(mOptionsOnClickListener);
		mEnterBtn.setOnClickListener(mOptionsOnClickListener);
	}
	private void initDirectionBtn(){
		mLeftBtn = (Button) findViewById(R.id.left);
		mRightBtn = (Button) findViewById(R.id.right);
		mUpBtn = (Button) findViewById(R.id.up);
		mDownBtn = (Button) findViewById(R.id.down);
		
		mLeftBtn.setOnClickListener(mDirectionOnClickListener);
		mRightBtn.setOnClickListener(mDirectionOnClickListener);
		mUpBtn.setOnClickListener(mDirectionOnClickListener);
		mDownBtn.setOnClickListener(mDirectionOnClickListener);
	}
	
	private void initNumBtn(){
		mNumView.findViewById(R.id.num_1).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_2).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_3).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_4).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_5).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_6).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_7).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_8).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_9).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_0).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_minus).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_pre).setOnClickListener(mNumOnClickListener);
	}
	
	private View.OnClickListener mDirectionOnClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			int code = -1;
			switch (v.getId()) {
				case R.id.up:
					code = KeyEvent.KEYCODE_DPAD_UP;
					break;
				case R.id.down:
					code = KeyEvent.KEYCODE_DPAD_DOWN;
					break;
				case R.id.left:
					code = KeyEvent.KEYCODE_DPAD_LEFT;
					break;
				case R.id.right:
					code = KeyEvent.KEYCODE_DPAD_RIGHT;
					break;
		
				default:
					break;
			}

			simulateKeyEvent(code);
		}
	};
	
	private View.OnClickListener mNumOnClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			int code = -1;
			switch (v.getId()) {
				case R.id.num_1:
					code = KeyEvent.KEYCODE_1;
					break;
				case R.id.num_2:
					code = KeyEvent.KEYCODE_2;
					break;
				case R.id.num_3:
					code = KeyEvent.KEYCODE_3;
					break;
				case R.id.num_4:
					code = KeyEvent.KEYCODE_4;
					break;
				case R.id.num_5:
					code = KeyEvent.KEYCODE_5;
					break;
				case R.id.num_6:
					code = KeyEvent.KEYCODE_6;
					break;
				case R.id.num_7:
					code = KeyEvent.KEYCODE_7;
					break;
				case R.id.num_8:
					code = KeyEvent.KEYCODE_8;
					break;
				case R.id.num_9:
					code = KeyEvent.KEYCODE_9;
					break;
				case R.id.num_0:
					code = KeyEvent.KEYCODE_0;
					break;
				case R.id.num_minus:
					code = KeyEvent.KEYCODE_0;
					break;
				case R.id.num_pre:
					code = KeyEvent.KEYCODE_0;
					break;
	
				default:
					break;
			}

			simulateKeyEvent(code);
		}
	};
	
	private View.OnClickListener mOptionsOnClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			int code = -1;
			switch (v.getId()) {
				case R.id.a:
					code = KeyEvent.KEYCODE_A;
					break;
				case R.id.b:
					code = KeyEvent.KEYCODE_B;
					break;
				case R.id.c:
					code = KeyEvent.KEYCODE_C;
					break;
				case R.id.d:
					code = KeyEvent.KEYCODE_D;
					break;
					
				case R.id.back:
					code = KeyEvent.KEYCODE_BACK;
					break;
				case R.id.enter:
					code = KeyEvent.KEYCODE_ENTER;
					break;
				case R.id.exit:
//					code = KeyEvent.KEYCODE_e;
					break;
		
				default:
					break;
			}
			
			simulateKeyEvent(code);
		}
	};
	
	private void simulateKeyEvent(int keyCode){
		RemoteKeyEvent keyDownEvent = new RemoteKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
		mController.println(keyDownEvent.toString());
		RemoteKeyEvent keyUpEvent = new RemoteKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode));
		mController.println(keyUpEvent.toString());
	}
}
